﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WarOfDev.EF;
using WarOfDev.Web.ViewModels;
using WarOfDev.Web.ViewModels.MappingToModel;
using WarOfDev.Web.Models.MappingToViewModel;

namespace WarOfDev.Web.Controllers
{
    public class ModuleController : Controller
    {
        private WarDevEntities db = new WarDevEntities();

        //
        // GET: /Module/

        public ActionResult Index()
        {
            return View(
                db.Modules
                .AsEnumerable()
                .Select(t => t.ToViewModel(
                                            db.Modules.SingleOrDefault(p => p.Id == t.ParentId),
                                            db.Modules.FirstOrDefault(p => p.ParentId == t.Id) == null
                                           )
                        ).ToList().OrderBy(p=>p.ParentId));
        }

        //
        // GET: /Module/Details/5

        public ActionResult Details(string id = null)
        {
            Module module = db.Modules.Single(p => p.Id == id);
            if (module == null)
            {
                return HttpNotFound();
            }
            return View(module);
        }

        //
        // GET: /Module/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Module/Create

        [HttpPost]
        public ActionResult Create(ModuleViewModel module)
        {
            if (ModelState.IsValid)
            {
                module.Id = Guid.NewGuid().ToString();
                module.CreatedOn = DateTime.Now;
                module.ModifiedOn = DateTime.Now;

                db.Modules.AddObject(module.ToModel());
                db.SaveChanges();

                return RedirectToAction("Index");
            }

            return View(module);
        }

        //
        // GET: /Module/Edit/5

        public ActionResult Edit(string id = null)
        {
            Module module = db.Modules.Single(p => p.Id == id);
            if (module == null)
            {
                return HttpNotFound();
            }

            return View(module.ToViewModel());
        }

        //
        // POST: /Module/Edit/5

        [HttpPost]
        public ActionResult Edit(ModuleViewModel module)
        {
            if (ModelState.IsValid)
            {
                module.ModifiedOn = DateTime.Now;
                var model = module.ToModel();
                db.Modules.Attach(model);
                db.ObjectStateManager.ChangeObjectState(model, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(module);
        }

        //
        // GET: /Module/Delete/5

        public ActionResult Delete(string id = null)
        {
            Module module = db.Modules.Single(p => p.Id == id);
            if (module == null)
            {
                return HttpNotFound();
            }
            return View(module);
        }

        //
        // POST: /Module/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {
            Module module = db.Modules.Single(p => p.Id == id);
            db.Modules.DeleteObject(module);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult ModuleList(string parentId)
        {
            List<SelectListItem> modules = new List<SelectListItem>();
            modules.Add(new SelectListItem { Text = string.Empty, Value = string.Empty, Selected = string.IsNullOrEmpty(parentId) });
            modules.AddRange(db.Modules.OrderBy(t => t.ParentId).Select(t => new SelectListItem { Text = t.ModuleName, Value = t.Id, Selected = string.IsNullOrEmpty(parentId) ? false : t.Id == parentId }).ToList());
            return PartialView("_ModuleList", modules);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}